//
//  FYUserPageCell.m
//  ThePainter
//
//  Created by Sean on 15/9/14.
//  Copyright (c) 2015年 FeiYu. All rights reserved.
//

#import "FYUserPageCell.h"
#import "FYPictureUtil.h"

@interface FYUserPageCell ()

@property (nonatomic, strong) UIImageView *img;
@property (nonatomic, strong) UIButton *btnLike;
@property (nonatomic, strong) UIButton *btnComment;
@property (nonatomic, strong) FYLabel *labLike;
@property (nonatomic, strong) FYLabel *labComment;
@property (nonatomic, strong) FYProduct *product;

- (void)initView;

@end

@implementation FYUserPageCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initView];
    }
    return self;
}

- (void)initView {
    
    UIView *container = [[UIView alloc] init];
    [self.contentView addSubview:container];
    [container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top);
        make.left.equalTo(self.contentView.mas_left);
        make.right.equalTo(self.contentView.mas_right);
        make.bottom.equalTo(self.contentView.mas_bottom);
    }];
    container.layer.cornerRadius = CGRectGetWidth(self.contentView.frame) / 100 * 5;
    container.layer.masksToBounds = YES;
    container.layer.borderWidth = 0.5;
    container.layer.borderColor = fyColorLightGary.CGColor;
    
    self.img = [[UIImageView alloc] init];
    [container addSubview:self.img];
    [self.img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(container.mas_top);
        make.left.equalTo(container.mas_left);
        make.right.equalTo(container.mas_right);
        make.height.mas_equalTo(CGRectGetWidth(self.contentView.frame));
    }];
    self.img.backgroundColor = [UIColor clearColor];
    
    UIView *bottomView = [[UIView alloc] init];
    [container addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.img.mas_bottom);
        make.left.equalTo(container.mas_left);
        make.right.equalTo(container.mas_right);
        make.bottom.equalTo(container.mas_bottom);
    }];
    bottomView.backgroundColor = [UIColor whiteColor];
    
    CGFloat bW = CGRectGetWidth(self.contentView.frame);
    CGFloat iconW = bW / 100 * 10;
    
    self.btnLike = [[UIButton alloc] init];
    [bottomView addSubview:self.btnLike];
    [self.btnLike mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(iconW);
        make.height.mas_equalTo(iconW);
        make.centerY.equalTo(bottomView.mas_centerY);
        make.left.equalTo(bottomView.mas_left).offset(bW / 100 * 20);
    }];
    self.btnLike.backgroundColor = [UIColor clearColor];
    [self.btnLike setImage:[UIImage imageNamed:@"icon_praise"] forState:UIControlStateNormal];
    
    
    self.labLike = [[FYLabel alloc] initWithString:@"0" size:fyLabSize2 color:fyColorLightGary];
    [bottomView addSubview:self.labLike];
    [self.labLike mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.btnLike.mas_right).offset(5);
        make.centerY.equalTo(bottomView.mas_centerY);
    }];
    
    self.labComment = [[FYLabel alloc] initWithString:@"0" size:fyLabSize2 color:fyColorLightGary];
    [bottomView addSubview:self.labComment];
    [self.labComment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bottomView.mas_right).offset( - bW / 100 * 20);
        make.centerY.equalTo(bottomView.mas_centerY);
    }];
    
    self.btnComment = [[UIButton alloc] init];
    [bottomView addSubview:self.btnComment];
    [self.btnComment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(iconW);
        make.height.mas_equalTo(iconW);
        make.centerY.equalTo(bottomView.mas_centerY);
        make.right.equalTo(self.labComment.mas_left).offset( - 5);
    }];
    [self.btnComment setImage:[UIImage imageNamed:@"icon_comment"] forState:UIControlStateNormal];
    self.btnComment.backgroundColor = [UIColor clearColor];
    
}

- (void)loadModel:(id)model {
    self.product = model;
    
    //设置SDWebImageManager
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    //下载图片
    [manager downloadImageWithURL:[NSURL URLWithString:self.product.productPic] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        self.img.image = [UIImage imageNamed:@"loading_small"];
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        if (image) {
            //处理图片
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                UIImage *newImage = [FYPictureUtil clipImageToSquare:image];
                //回到主线程，显示图片
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.img.image = newImage;
                    self.isLoadModel = YES;
                });
            });
        }
    }];
    
    [self.labLike setStringAndLeft:self.product.likeCount];
    [self.labComment setStringAndLeft:self.product.commentCount];
}

@end
